<?php
require_once('../../config.php');
Doctrine::loadModels('../../models');

// can combine agent and people into query to return one result set.
// wrote SQL select as unsure of doctrine method when multiple references to same table
if ($_POST["agent_id"]) 
{
	$agent_id = strip_tags($_POST["agent_id"]);
	//file_put_contents  ( "param_id.txt", $agent_id);	
	
	$query = $db_query = sprintf("SELECT a.agent_id, a.name_short, a.name_full, a.agent_type_id, a.description, a.date_start, a.date_end,
        e.event_type_id, e.event_status_id, e.language_id, e.event_audience_id,    
        type.item_name event_type, status.item_name event_status, l.item_name language, 
        audience.item_name event_audience 
	FROM gbif_events e
  		INNER JOIN agent a ON e.agent_event_id = a.agent_id
  		INNER JOIN lk_glossary type ON e.event_type_id = type.item_id
  		INNER JOIN lk_glossary status ON e.event_status_id = status.item_id
  		INNER JOIN lk_glossary audience ON e.event_audience_id = audience.item_id
  		INNER JOIN lk_glossary l ON e.language_id = l.item_id 
	WHERE a.agent_id ='".$agent_id."'");
	
	$items = mysql_query($query);	
}

$dom = new DomDocument('1.0',"UTF-8");
$dom->formatOutput = true;
$root = $dom->createElement( "results" );
$dom->appendChild( $root );
	while(($item = mysql_fetch_assoc($items)))
	{
		$dn = $dom->createElement( "record" );
		$dn->setAttribute( 'agent_id', ($item['agent_id']));
		$dn->setAttribute( 'name_short', ($item['name_short']));
		$dn->setAttribute( 'name_full', ($item['name_full']));
		$dn->setAttribute( 'description', ($item['description']));
		$dn->setAttribute( 'date_start', $item['date_start'] );
		$dn->setAttribute( 'date_end', $item['date_end'] );
		$dn->setAttribute( 'event_type_id', $item['event_type_id'] );
		$dn->setAttribute( 'event_status_id', $item['event_status_id'] );
		$dn->setAttribute( 'language_id', $item['language_id'] );
		$dn->setAttribute( 'event_audience_id', ($item['event_audience_id']));
		$dn->setAttribute( 'event_type', ($item['event_type']));
		$dn->setAttribute( 'event_status', ($item['event_status']));
		$dn->setAttribute( 'event_audience', ($item['event_audience']) );
		$dn->setAttribute( 'language', ($item['language']) );
		$root->appendChild( $dn );
	}		
header( "Content-type: text/xml; charset=UTF-8");
echo $dom->saveXML();

?>